package com.ebupt.migu.music.api.controller;

import com.ebupt.migu.music.api.service.HiveToRedisService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @Description Hive以文件形式向Redis中写入数据
 * @Author liuyangyang@cd.ebupt.com
 * @Date 2020/7/16 16:03
 */
@Slf4j
@RestController
@RequestMapping("/hive2redis")
public class HiveToRedisController {

    @Autowired
    private HiveToRedisService hiveToRedisService;

    /**
     * 存储用户信息的key，内容形式为：
     * key ： user_info
     * value ： map<key, value>
     * 关于map：
     * key：手机号或uid
     * value：用户姓名
     * 例： "13688361924":"刘洋洋"
     */
    private final String _REDIS_KEY = "_user_info";

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 将用户信息录入Redis
     *
     * @param path
     * @throws IOException
     */
    @RequestMapping(value = "/username", method = RequestMethod.POST)
    public void userName(@RequestParam(value = "path", required = false) String path) throws IOException {
        hiveToRedisService.userName(path);
    }

    /**
     * 查询指定用户的昵称
     *
     * @param msisdn
     * @return
     */
    @RequestMapping(value = "/getuser", method = RequestMethod.POST)
    public String getUser(@RequestParam(value = "msisdn", required = false) String msisdn) {
        BoundHashOperations boundHashOperations = redisTemplate.boundHashOps(_REDIS_KEY);
        String nickName = String.valueOf(boundHashOperations.get(msisdn));
        return nickName;
    }

}
